#include<bits/stdc++.h>
using namespace std;

bool check(vector<int> idx) {
	for (int i = 1; i < idx.size(); i++)
		if (idx[i] - idx[i - 1] != 1)
			return false;
	return true;
}

signed main() {
	string s;
	int t, n;
	cin >> t;
	while (t--) {
		cin >> n;
		cin >> s;
		vector<int> idx;
		int ans = 0;
		for (int i = 0; i < s.size(); i++)
			if (s[i] == '@')	idx.push_back(i);

		for (int i = 0; i < s.size(); i++) {
			if (s[i] == '_') {
				int index = lower_bound(idx.begin(), idx.end(), i) - idx.begin();
				if (2 * index > idx.size()) {
					for (int j = 0; j < index; j++)
						idx[j]++, ans ++;
				} else {
					for (int j = index; j < idx.size(); j++)
						idx[j]--, ans++;
				}
				if (check(idx))
					break;
			}
		}
		cout << ans << endl;
	}
	return 0;
}
